// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import router from './router'
import store from './store/index'
import App from './App'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import 'swiper/dist/css/swiper.css'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import { AlertPlugin, AjaxPlugin, ConfirmPlugin, DatetimePlugin, ToastPlugin, LoadingPlugin } from 'vux'
import { InfiniteScroll, Spinner, Indicator } from 'mint-ui'
import SixButton from './components/public/SixButton'
import http from './utils/http'
import { getStore } from './utils/mUtils.js'

// mint-ui按需引入组件
Vue.use(InfiniteScroll)
Vue.use(Indicator)
// 全局调用Indicator
Vue.prototype.$Indicator = Indicator 
Vue.component(Spinner.name, Spinner)

// 自定义公用全局按钮组件
Vue.component('six-button', SixButton)


Vue.use(ToastPlugin)
Vue.use(LoadingPlugin)
Vue.use(DatetimePlugin)
Vue.use(ConfirmPlugin)
Vue.use(AlertPlugin)
Vue.use(AjaxPlugin)
Vue.use(VueAwesomeSwiper)

// 服务器地址
if (process.env.NODE_ENV == 'development') {	
	// 开发环境
  global.serverHost = ""
}else{
	// 生产环境
  global.serverHost = "http://yumer.org"
}

FastClick.attach(document.body)
// NProgress顶部进度加载配置
NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })

// 获取储存在本地的token
global.token = getStore('token')
router.beforeEach((to, from, next) => {
	NProgress.start()
  document.title = to.meta.title
  if (to.meta.requireAuth) {
    if (global.token) {
      next()
    }else {
      next({ path: '/login' })
    } 
  }else {
    if (global.token) {
      if (to.path == '/login' || to.path == '/register') {
        next({ path: '/' })
      }else {
        next()
      }
    }else {
      next()
    }
  }
})


// 初始化用户
if (global.token) {
  http.get('/api/v1/getUserInfo', {}, false, res => {
    if (res.status == 1) {
      store.dispatch('saveUserInfo', res.userInfo)
    }
  })
  store.dispatch('getCartLen')
}

router.afterEach(() => {
  NProgress.done()
})

Vue.config.productionTip = false

/* eslint-disable no-new */
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app-box')
